package com.example.navigation.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.navigation.api.statistic.RoadRank
import com.example.navigation.viewModel.StatisticsViewModel

/**
 * Created with IntelliJ IDEA.
 * @Author: Liao Run
 * @Date: 2024-06-11 15:25
 * @Description:
 **/
@Composable
fun RoadRankDialog(statisticsViewModel: StatisticsViewModel = hiltViewModel()) {

    var roadRank by remember {
        mutableStateOf<RoadRank>(emptyArray())
    }
    LaunchedEffect(key1 = Unit) {
        statisticsViewModel.roadRank.collect {
            roadRank = it
        }
    }

    if (roadRank.isEmpty()) {
        return
    }
    Box(modifier = Modifier.fillMaxSize()) {
        LazyVerticalGrid(
            modifier = Modifier
                .align(Alignment.BottomEnd)
                .width(200.dp)
                .height(200.dp)
                .background(Color.White)
                .zIndex(1f),
            columns = GridCells.Fixed(2),
        ) {
            item {
                Text(text = "道路名", color = Color.Blue)
            }
            item {
                Text(text = "积尘值", color = Color.Blue)
            }

            roadRank.map {
                item {
                    Text(text = it.roadName)
                }

                item {
                    Text(text = it.avgPm)
                }
            }
        }
    }

}